package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mask.im.business.model.entity.UserAuth;
import com.mask.im.business.model.entity.UserInfo;
import com.mask.im.business.common.exception.BusinessException;
import com.mask.im.business.mapper.SysRoleMapper;
import com.mask.im.business.mapper.UserAuthMapper;
import com.mask.im.business.mapper.UserInfoMapper;
import com.mask.im.business.model.dto.UserDetailsDTO;
import com.mask.im.business.service.RedisService;
import com.mask.im.business.common.util.IpUtil;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

import static com.mask.im.business.common.constant.CommonConstant.*;
import static com.mask.im.business.common.constant.RegisterConstant.LOGIN_FAILURE;
import static com.mask.im.business.common.constant.RegisterConstant.LOGIN_FAILURE_RESTRICTIONS;

@Service
public class UserDetailsServiceImpl {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAuthMapper userAuthMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private HttpServletRequest request;

    public Object loadUserByUsername(String account)  {
        Object isExists = redisService.hGet(LOGIN_FAILURE, LOGIN_FAILURE_RESTRICTIONS + account);
        if (Objects.nonNull(isExists)) {
            throw new BusinessException("账户已被限制登入，请稍后重试");
        }
        UserAuth userAuth = userAuthMapper.selectOne(new LambdaQueryWrapper<UserAuth>()
                .eq(UserAuth::getUsername, account).or()
                .eq(UserAuth::getEmail, account).or()
                .eq(UserAuth::getPhone, account));
        if (Objects.isNull(userAuth)) {
            throw new BusinessException("输入的账号不存在");
        }
//		Object isPass = redisService.get(SLIDER_PASS_ACCOUNTLOGIN + account);
//		if (Objects.isNull(isPass) || Integer.parseInt(isPass.toString())!=1){
//			throw new BusinessException("校验未通过，请重新进行校验");
//		}
        redisService.del(SLIDER_PASS_ACCOUNTLOGIN + account);
        return convertUserDetail(userAuth, request);
    }

    public UserDetailsDTO convertUserDetail(UserAuth userAuth, HttpServletRequest request) {

        UserInfo userInfo = userInfoMapper.selectById(userAuth.getUserInfoId());

        List<String> roles = sysRoleMapper.selectRoleList(userAuth.getUserInfoId());
        String ipAddress = IpUtil.getIpAddress(request);

        String ipSource = IpUtil.getIpSource(ipAddress);

        UserAgent userAgent = IpUtil.getUserAgent(request);

        return UserDetailsDTO.builder()
                .username(userAuth.getUsername())
                .password(userAuth.getPassword())
                .email(userAuth.getEmail())
                .phone(userAuth.getPhone())
                .avatar(userInfo.getAvatar())
                .disable(userInfo.getDisable())
                .id(userInfo.getId())
                .intro(userInfo.getIntro())
                .ipAddress(ipAddress)
                .os(userAgent.getOperatingSystem().getName())
                .browser(userAgent.getBrowser().getName())
                .ipSource(ipSource)
                .lastLoginTime(userAuth.getLastLoginTime())
                .nickName(userInfo.getNickName())
                .roles(roles)
                .gender(userInfo.getGender())
                .build();
    }
}
